<!DOCTYPE html>
<html>
<head>
<#include "/common/freemarker/include_css.ftl">
<#include  "/app/common/uploadimage/attachment.ftl">
<title>活动奖品配置</title>
</head>
 
 <#assign grades=["一等奖","二等奖","三等奖"]>
<body>
	<div class="panel panel-default">
	   <div class="panel-heading">
	       <div class="panel-title">活动奖品配置</div>
	   </div>
	   <div clas="panel-body">
	      <div class="row">
	           <div class="col-md-12">
	               <br>
	               
	               <div  id="prize"  name="prize" style="display:none">
			 				<input type="hidden" name="id" id="id_NO" value='' />
		                    <div class="form-group">
		                    	<label class="col-md-2 control-label">奖品等级 <font color="red"> *</font></label>
		                        <div class="col-md-9">
		                        	<select class="form-control" id="grade_NO" name="grade">
		                           <#list grades as grade>
		                           		<option value="${grade}" >${grade}</option>
		                           </#list>
								   </select>
		                    	</div>              	
		                    </div> 
		                    <div class="form-group">
		                    	<label class="col-md-2 control-label">奖品名称 <font color="red"> *</font></label>
		                        <div class="col-md-9">
									<input type="text"  class="input-sm form-control required" id="name_NO" name="name" value=''      />
		                    	</div>              	
		                    </div> 
		                     <div class="form-group">
		                    	<label class="col-md-2 control-label">红包奖金 <font color="red"> *</font></label>
		                        <div class="col-md-9">
									<input type="text"  class="input-sm form-control required number" id="money_NO" name="money" value=''    />
		                    	</div>              	
		                    </div> 
		                    
		                     <div class="form-group">
		                    	<label class="col-md-2 control-label">中奖率(%) <font color="red"> *</font></label>
		                        <div class="col-md-9">
									<input type="text"  class="input-sm form-control required number" id="hitRatio_NO"  name="hitRatio" value=''    />
		                    	</div>              	
		                    </div> 	
             
		                    <div class="form-group">
		                    	<label class="col-md-2 control-label">奖品图片<font color="red"></font></label>
		                        <div class="col-md-9">
		                           
		    
		                     
						            <controls>
										<div class="container-fluid">
											<div class="thumbnail"> 
										        <div class="imgs" > 
										            <img id="img_NO" src="<@context/>images/uploadphoto.png"  onclick="attachWindow2('fileid_NO','filename_NO')">		
										        </div> 
										        <div class="caption"> 
										            <div class="title">详情图</div> 
										            <div class="content"> 
										                建议尺寸：宽400px，高300px
										            </div> 
										            <div class="author"> 
										            	<a name="btnUpload"  class="btn btn-info" href="#" onclick="attachWindow2('fileid_NO','filename_NO')">上传</a>
										            </div> 
										        </div> 
										    </div> 
										</div>
									</controls>
								  <input type="hidden" id="filename_NO" name="filename_NO" value=""    class="form-control "  data-type="attachment"/>
							      <input type="hidden" id="fileid_NO" name="image" value="">
							      

									 
				 
							     </div>              	
		                    </div>
		                    
		                     <div class="form-group">
		                    	<div class="col-md-offset-2 col-md-10">
		                    		
		                    		<input type="button" class="btn btn-sm btn-default" value="删除当前奖品" onclick="deletePrizeDiv(this)"/>
		                    	</div>
		                    </div>
		                  </div> 
		                  
		                  
	               <form id="contentForm" class="form-horizontal"  action="" method="post"  >
	                	<input type="hidden" name="eventUuid" id="eventUuid" value='${event.uuid}' />
	 
						
					<div id="parentpoint">
						
						
						<#list prizeList as prize>
						<div  id="prize${prize_index}"  name="prize${prize_index}">
							<input type="hidden" name="id" id="id" value='${prize.id?if_exists}' />
		                    <div class="form-group">
		                    	<label class="col-md-2 control-label">奖品等级 <font color="red"> *</font></label>
		                        <div class="col-md-9">
		                        	<select class="form-control" id="grade_${prize_index}" name="grade">
		                           <#list grades as grade>
		                           		<option value="${grade}" <#if prize.grade?default("")==grade>selected</#if>>${grade}</option>
		                           </#list>
								   </select>
		                    	</div>              	
		                    </div> 
		                    <div class="form-group">
		                    	<label class="col-md-2 control-label">奖品名称 <font color="red"> *</font></label>
		                        <div class="col-md-9">
									<input type="text"  class="input-sm form-control required" id="name_${prize_index}" name="name" value='${prize.name?default("")}'    />
		                    	</div>              	
		                    </div> 
		                     <div class="form-group">
		                    	<label class="col-md-2 control-label">红包奖金 <font color="red"> *</font></label>
		                        <div class="col-md-9">
									<input type="text"  class="input-sm form-control required number" id="money_${prize_index}" name="money" value='${prize.money?default("")}'      />
		                    	</div>              	
		                    </div> 
		                    
		                     <div class="form-group">
		                    	<label class="col-md-2 control-label">中奖率(%) <font color="red"> *</font></label>
		                        <div class="col-md-9">
									<input type="text"  class="input-sm form-control required number"  id="hitRatio_${prize_index}"  name="hitRatio" value='${prize.hitRatio?default("")}'      />
		                    	</div>              	
		                    </div> 	  
		                                
		                    <div class="form-group">
		                    	<label class="col-md-2 control-label">奖品图片<font color="red"></font></label>
		                        <div class="col-md-9">
		                            <#assign tmps="">
		                        	<#if prize?exists>
										<#assign tmps=prize.image?if_exists>
										<#assign imagedownladUrl= "common/download.do?id="+tmps  >
									<#else>
										<#assign imagedownladUrl= "images/uploadphoto.png">	
		                        	</#if>
 
		                   			 <@imageAttachment  id="image${prize_index}" name="image" value=tmps namePrefix=""  showType="all"   /> 
							     </div>              	
		                    </div>
		            
		                     <div class="form-group">
		                    	<div class="col-md-offset-2 col-md-10">
		                    		
		                    		<input type="button" class="btn btn-sm btn-default" value="删除当前奖品" onclick="deletePrizeDiv(this)"/>
		                    	</div>
		                    </div>
		                  </div> 
						</#list>
	                	
	                </div>	    
	                    
	                    <div class="form-group">
	                    	<div class="col-md-offset-2 col-md-10">
	                    	    <input type="button" class="btn btn-sm btn-primary" id="copyPrize" value="新增奖品" onclick="AddPrizeDiv('prize')"/>
	                    		<input type="button" class="btn btn-sm btn-primary" id="submitBtn" value="提交保存" onclick="javascript:doSave()"/>
	                    		<input type="button" class="btn btn-sm btn-default" value="返 回" onclick="javascript:doBack()"/>
	                    	</div>
	                    </div>
	              </form>
	        </div>
	    </div>
	</div>
	<#--  公共js -->
	<#include "/common/freemarker/include_js.ftl">
	<#--  页面自己的js -->
	
	<script type="text/javascript" src="<@context/>js/mindao/mindao.tree.js"></script>

	 <script >	

	 	String.prototype.replaceAll  = function(s1,s2){  
			return this.replace(new RegExp(s1,"gm"),s2);   //这里的gm是固定的，g可能表示global，m可能表示multiple。 
		}
 
		$(function () {
		if ($.validator) {
		    //fix: when several input elements shares the same name, but has different id-ies....
		    $.validator.prototype.elements = function () {
		        var validator = this,
		            rulesCache = {};
		        // select all valid inputs inside the form (no submit or reset buttons)
		        // workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
		        return $([]).add(this.currentForm.elements)
		        .filter(":input")
		        .not(":submit, :reset, :image, [disabled]")
		        .not(this.settings.ignore)
		        .filter(function () {
		            var elementIdentification = this.id || this.name;
		            !elementIdentification && validator.settings.debug && window.console && console.error("%o has no id nor name assigned", this);
		            // select only the first element for each name, and only those with rules specified
		            if (elementIdentification in rulesCache || !validator.objectLength($(this).rules()))
		                return false;
		            rulesCache[elementIdentification] = true;
		            return true;
		        });
		    };
		}
		});
		//动态生成和删除奖品的div
		var prizediv =#{prizeList?size};
		//生成奖品div
		function AddPrizeDiv(strid){
			 	var o=document.getElementById(strid);	 
		        var div=document.createElement("div");
		        div.id = "prize"+ prizediv; 
				
		        <!--div.innerHTML=o.innerHTML.replace(/\{0\}/ig,a);-->
				//将现有的divhtml赋值给新创建的div
				
	 			var tmpStr=o.innerHTML;
	 			tmpStr = tmpStr.replaceAll("_NO",div.id)
				 
			 
				div.innerHTML=tmpStr;
				document.getElementById("parentpoint").appendChild(div);
				//给新的div的删除按钮赋值（动态加1）
 
		        prizediv++;
 
		}
		//动态删除奖品div
		function deletePrizeDiv(obj)
		{	
			<!--var strid=obj.name;-->
			//获取本删除按钮的id值，并创建对象
			
			var z=obj.parentElement.parentElement.parentElement;
			<!--var o=document.getElementsByTagName(strid)[4].parentElement;-->
			//找到这个删除按钮对应的父div的值
			<!--var stridone=document.getElementsByName(strid)[0].parentNode.id;-->
			var stridone=z.id;
 
			//判断本id对应的值并删除
			var my = document.getElementById(stridone);
			if (my != null){
			my.parentNode.removeChild(my);}
			<!--window.location.reload(); -->
		}
		
		
		    
	function doSave(){	
	 	  
		  if(!$("#contentForm").valid()){
		    	 return;
		  }
		  <#--  
		  if ($("#roleType").val()==1){
		  	  if ($("#positionId").val()==""){
		  	  }
		  	  if ($("#positionId").val()==""){
		  	  }
		  }
		  -->
		  var options = {
  			formId : 'contentForm',
			url : '<@context/>ued/eventprize/savePrize.do',
			callBack : function(jsonRst) {
				if (jsonRst.status=="success") {
					 alert("保存成功");
					 window.location.href="<@context/>ued/event/search.do";
				 
	      		} else {
	      			alert( "出错了！"+jsonRst.message);
	      		}
			}
		};
		comm_saveInfo(options);
	}
	
	function doBack(){
		window.location.href="<@context/>ued/event/search.do";
	}
	
 
	</script>
</body>
</html>
 

